package com.mutil_bindservice2;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;

import com.mutilbindservice2.R;
import com.testmodelconnectionapp.MClientAIDL;
import com.testmodelconnectionapp.MServiceAIDL;
import com.testmodelconnectionapp.MsgBean;

import java.util.Random;

public class MainActivity extends Activity {
    private static final String TAG = "mutil_MainActivity";
    private MServiceAIDL mServiceAIDL;
    private ServiceConnection conn = new ServiceConnection() {

        //绑定上服务的时候
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.i(TAG, "onServiceConnected: ----");
            //拿到了远程的服务
            mServiceAIDL = MServiceAIDL.Stub.asInterface(service);
            MClientAIDL.Stub stub = new MClientAIDL.Stub() {
                @Override
                public String getServerInfo(MsgBean msgBean, String str) throws RemoteException {
                    return null;
                }

                @Override
                public float[] getFloats( String str) throws RemoteException {
                    Log.i(TAG, "getFloats: ");
                    return {10.1f,20.1f,30.1f};;
                }
            };
            try {
                //向服务端 传递binder
                mServiceAIDL.getServerInfo(stub);
            } catch (RemoteException e) {
                e.printStackTrace();
            }

        }

        //断开服务
        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.e(TAG, "onServiceDisconnected: ----" + name.flattenToShortString());
            //回收资源
            mServiceAIDL = null;
        }
    };
    private boolean bindServiceResult;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void bindService(View view) {
        Intent intent1 = new Intent();
        intent1.setComponent(new ComponentName("com.my.screenadapter","com.testmodelconnectionapp.AIDLService"));
//        intent1.setPackage(this.getPackageName());
        bindServiceResult = bindService(intent1, conn, Context.BIND_AUTO_CREATE);
        Log.i(TAG, "bindService: "+bindServiceResult);
    }

    public void mUnbindService(View view) {
        if (conn!=null && bindServiceResult){
            unbindService(conn);
            bindServiceResult = false;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mUnbindService(null);
    }

    public void sendReqToService(View view) {
        final MsgBean inBean = new MsgBean();
        inBean.setMsg1("in client");
        final MsgBean outBean = new MsgBean();
        outBean.setMsg1("out client");
        final MsgBean inoutBean = new MsgBean();
        inoutBean.setMsg1("inout client");


        String add = null;
        try {
            if (mServiceAIDL == null) {
                Log.i(TAG, "sendReqToService: mServiceAIDL == null");
                return;
            }
            add = mServiceAIDL.getClientInfo(
                    new Random(100).nextInt(),
                    new Random(100).nextInt(),
                    inBean);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
        Log.i(TAG, "sendReqToService: result app2:"+add);
    }
}
